#!/usr/bin/perl
require "accounts.pl";

local @user_name_list;
local %user_fullname_list;
local %user_disable_list;
local $table_lines = 0;

&header($text{'index_title'}, "", "index", 0, 1);

@user_name_list = &accounts_user_list(\%user_fullname_list, \%user_disable_list);

print "<form name=indexform action=index.cgi method=post onsubmit='return onsubmit_form();'>\n";
print "<table class=mode1 border=0 width=100% cellpadding=2 cellspacing=0>\n";
print "<tr><td width=80% valign=top>\n";
print "<div style='valign:top;width:100%;height:300;overflow:auto'>";
print "<table class=mode1 width=100% border=0 cellpadding=2 cellspacing=0>\n";
print "<tr class=title1><th align=left width = 33%> $text{index_tr_name} </th><th align=left width = 33%> $text{index_tr_full} </th>\n";
print "<th align=left width = 33%> $text{index_tr_disable} </th></tr>\n";

#output @user_list

if(@user_name_list > 0)
{
	my $i = 0;
	my $trclass = "";
	while($i < @user_name_list)
	{
		if($i % 2)
		{
			$trclass = "class=mode2";
		}
		else
		{
			$trclass = "class=mode1";
		}
		print "<tr $trclass><td><input type='radio' name=username value=\"$user_name_list[$i]\" onclick=\'document.forms[0].fullname.value=\"$user_fullname_list{$user_name_list[$i]}\"; document.forms[0].disabled.value=\"$user_disable_list{$user_name_list[$i]}\"; change_button_disable_value(\"$user_disable_list{$user_name_list[$i]}\", \"$text{'index_submit_disable'}\", \"$text{'index_submit_enable'}\");\'>$user_name_list[$i]</td>";
		print "<td>$user_fullname_list{$user_name_list[$i]}</td>";
		print "<td>$text{'disable_'.$user_disable_list{$user_name_list[$i]}}</td></tr>\n";
		$table_lines++;
		$i++;
	}
}

print "</table>\n";

print "<input name=buttonsubmit type=hidden value=''>\n";
print "<input name=fullname type=hidden value=''>\n";
print "<input name=disabled type=hidden value=''>\n";

print "</div>";

#the next td for button
print "</td><td width=2%>\n";
print "</td><td width=18% align=center valign=top>\n";

print "<table class=mode1>\n";

print "<tr><td width=25%><input name=submit_create type='submit' value=\"$text{'index_submit_create'}\" class='button' onclick=\'return on_create_click(\"$text{'error_msg_noselect_item'}\");\'></td></tr>\n";
print "<tr><td width=25%><input name=submit_modify type='submit' value=\"$text{'index_submit_modify'}\" class='button' onclick=\'return on_modify_click($table_lines, \"$text{'error_msg_noselect_item'}\");\'></td></tr>\n";
print "<tr><td width=25%><input name=submit_delete type='submit' value=\"$text{'index_submit_delete'}\" class='button' onclick='return on_delete_click($table_lines, \"$text{'error_msg_noselect_item'}\", \"$text{'index_delete_msg'}\");'></td></tr>\n";
print "<tr><td width=25%><input name=submit_disabled type='submit' value=$text{'index_submit_disable'} class='button' onclick='return on_disable_click($table_lines, \"$text{'error_msg_noselect_item'}\");'></td></tr>\n";
print "</table>\n";

print "</td><tr>\n";
print "</table>\n";

print "</form>\n";

#print "<hr>";

print <<EOF;
<script>
function onsubmit_form()
{
        document.forms[0].submit_create.disabled = true;
        document.forms[0].submit_modify.disabled = true;
        document.forms[0].submit_delete.disabled = true;
        document.forms[0].submit_disabled.disabled = true;
}

function change_button_disable_value(is_disable, disable_v, enable_v)
{
	if(is_disable == "yes")
	{
		document.forms[0].submit_disabled.disabled = false;
		document.forms[0].submit_disabled.value = enable_v;
	}
	if(is_disable == "no")
	{
		document.forms[0].submit_disabled.disabled = false;
		document.forms[0].submit_disabled.value = disable_v;
	}
	if(is_disable == "smb_err")
	{
		document.forms[0].submit_disabled.disabled = true;
	}
}

function on_create_click()
{
	//alert('asdf');
	document.forms[0].action = 'edit_user.cgi';
	document.forms[0].buttonsubmit.value = 'create';
	//document.forms[0].submit();
	return true;
}

function on_modify_click(length, error_msg)
{
	var i;
	var find = 0;

	if(length < 2)
	{
		if(length < 1)
		{
			return false;
		}
		else
		{
			if(document.forms[0].username.checked)
			{
				find = 1;
			}
		}
	}
	else
	{
		for(i = 0; i < length; i++)
		{
			if(document.forms[0].username[i].checked)
			{
				find = 1;
			}
		}
	}

	if(find == 0)
	{
		alert(error_msg);
		return false;
	}

	document.forms[0].action = 'edit_user.cgi';
	document.forms[0].buttonsubmit.value = 'modify';
	//document.forms[0].submit();
}

function on_delete_click(length, error_msg, delete_msg)
{
	var i;
	var find = 0;

	if(length < 2)
	{
		if(length < 1)
		{
			return false;
		}
		else
		{
			if(document.forms[0].username.checked)
			{
				find = 1;
			}
		}
	}
	else
	{
		for(i = 0; i < length; i++)
		{
			if(document.forms[0].username[i].checked)
			{
				find = 1;
			}
		}
	}

	if(find == 0)
	{
		alert(error_msg);
		return false;
	}

        if(!confirm(delete_msg))
        {
                return false;
        }

	document.forms[0].action = 'delete_user.cgi';
}

function on_disable_click(length, error_msg)
{
	var i;
	var find=0;

	if(length < 2)
	{
		if(length < 1)
		{
			return false;
		}
		else
		{
			if(document.forms[0].username.checked)
			{
				find = 1;
			}
		}
	}
	else
	{
		for(i = 0; i < length; i++)
		{
			if(document.forms[0].username[i].checked)
			{
				find = 1;
			}
		}
	}

        if(find == 0)
        {
                alert(error_msg);
		return false;
        }
	document.forms[0].action = 'disable_user.cgi';
}

</script>
EOF

&footer("/", $text{'index'});
